﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAO;
using DTO;

namespace BUS
{
    public class ReceivingBUS
    {
        ReceivingDAO receiDAO = new ReceivingDAO();

        public bool InsertReceivingList(ReceivingListDTO receiDto)
        {
            try
            {
                receiDto.ReceivingID = receiDAO.InsertReceiving(receiDto);
                if (receiDto.ReceivingID > 0)
                {
                    foreach (ReceivingDetailDTO receiDetail in receiDto.ListReceiving)
                    {
                        receiDetail.ReceivingID = receiDto.ReceivingID;
                        receiDetail.ReceivingDetailID = receiDAO.InsertReceivingDetail(receiDetail);

                        BookDAO bookDAO = new BookDAO();
                        bookDAO.UpdateQuantityFromReceiving(receiDetail.BookID, receiDetail.Quantity);
                    }
                    return true;
                }
                else
                {
                    return false;
                }                
            }
            catch
            {
                return false;
            }
        }

        public bool CheckQuantityReceiving(int quantity)
        {
            ParameterDAO paraDAO = new ParameterDAO();
            int paraQuantity = paraDAO.GetParameterValueByCode(ParameterCode.MaxReceiving.ToString());

            if (quantity > paraQuantity)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
